package com.huike.task.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.constant.MagicConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Description TaskMapper
 * @Author zhuzi
 * @Date 2023/10/17
 */
@Component
@RequiredArgsConstructor
public class TaskUtil {

    private final TbAssignRecordMapper tbAssignRecordMapper;

    private final TbClueMapper tbClueMapper;

    private final TbBusinessMapper tbBusinessMapper;

    /**
     * 根据时间获取线索相关信息
     *
     * @param time 回收与提醒时间
     * @return 返回分配id和线索id
     */
    public List<TbAssignRecord> getClueListByTime(Integer time) {
        LocalDateTime ldt = LocalDateTime.now();
        LambdaQueryWrapper<TbAssignRecord> qClueRecovery = new LambdaQueryWrapper<>();
        qClueRecovery.select(TbAssignRecord::getId).
                select(TbAssignRecord::getAssignId).
                eq(TbAssignRecord::getLatest, "1").
                eq(TbAssignRecord::getType, MagicConstants.CLUE).
                ge(TbAssignRecord::getCreateTime, ldt.minusHours(time));
        return tbAssignRecordMapper.selectList(qClueRecovery);
    }

    /**
     * 根据时间获取商机相关信息
     *
     * @param time 回收与提醒时间
     * @return 返回分配id和商机id
     */
    public List<TbAssignRecord> getBusinessListByTime(Integer time) {
        LocalDateTime ldt = LocalDateTime.now();
        LambdaQueryWrapper<TbAssignRecord> qBusinessRemind = new LambdaQueryWrapper<>();
        qBusinessRemind.select(TbAssignRecord::getId).
                select(TbAssignRecord::getAssignId).
                eq(TbAssignRecord::getLatest, "1").
                eq(TbAssignRecord::getType, MagicConstants.BUSINESS).
                ge(TbAssignRecord::getCreateTime, ldt.minusHours(time));
        return tbAssignRecordMapper.selectList(qBusinessRemind);
    }

    /**
     * 修改分配表
     *
     * @param ids 分配表id集合
     */
    public void updateAssign(List<Long> ids) {
        tbAssignRecordMapper.updateLeastByIds(ids);
    }

    /**
     * 修改线索表
     *
     * @param ids 线索表id集合
     */
    public void updateClue(List<Long> ids) {
        tbClueMapper.updateStatusByIds(TbClue.builder().status("3").updateTime(new Date()).build(), ids);
    }

    /**
     * 修改商机表
     *
     * @param ids 商机表id集合
     */
    public void updateBusiness(List<Long> ids) {
        tbBusinessMapper.updateStatusByIds(TbBusiness.builder().status("3").lastUpdateTime(new Date()).build(), ids);
    }

    /**
     * 线索提醒
     *
     * @param ids 线索id集合
     */
    public void clueRemind(List<Long> ids) {
        // todo: 线索提醒
    }

    /**
     * 商机提醒
     *
     * @param ids 商机id集合
     */
    public void businessRemind(List<Long> ids) {
        // todo: 商机提醒
    }

}
